package com.leetcode.week.week291;

import java.util.Arrays;

/**
 * @author angzhijin
 * @Description
 * @create 2022-05-01 11:18 上午
 */
public class LeetCodeWeek4 {
    public static void main(String[] args) {
        System.out.println(appealSum("abbca"));
        System.out.println(appealSum("code"));
    }

    public static long appealSum(String s) {
        if(s.length()==1) return 1;
        int len = s.length();
        long[] count = new long[26];
        long sum=0, prv =0;
        for (int i = 0; i < len; i++) {
            int ch = s.charAt(i)-'a';
            prv += i+1 - count[ch]; // 在前一位的基础上加上自己，同时去掉上一位的出现次数
            sum+=prv;
            count[ch] = i+1;
        }
        return sum;
    }
}
